package com.beiding.businessplatform.ql;

import java.util.*;

class QueryBuilderImpl implements ConnectorAble, QueryBuilder {

    private List<Unit> units = new ArrayList<>();
    private Set<QueryField> fields = new HashSet<>();

    QueryBuilderImpl() {

    }

    @Override
    public Set<QueryField> getQueryFields() {
        return fields;
    }

    public ConnectorAble eq(QueryField queryField, Object value) {
        units.add(new Unit() {
            @Override
            public void handle(BuilderHolder holder) {
                String key = holder.addSet(value);
                holder.appendWhere(" ").appendWhere(queryField.getKey()).appendWhere(" = :").appendWhere(key).appendWhere(" ");
                fields.add(queryField);
            }
        });
        return this;
    }


    public QueryBuilder or() {
        units.add(new Connector() {
            @Override
            public void handle(BuilderHolder holder) {
                holder.appendWhere(" or ");
            }
        });
        return this;
    }

    public QueryBuilder and() {
        units.add(new Connector() {
            @Override
            public void handle(BuilderHolder holder) {
                holder.appendWhere(" and ");
            }
        });
        return this;
    }

    //括号包裹
    public ConnectorAble wrap(QueryBuilder queryBuilder) {
        units.add(new Unit() {
            @Override
            public void handle(BuilderHolder holder) {
                holder.appendWhere(" ( ");
                queryBuilder.build(holder);
                holder.appendWhere(" ) ");
            }
        });
        return this;
    }

    public ConnectorAble or(QueryBuilder queryBuilder) {
        or();
        return wrap(queryBuilder);
    }

    @Override
    public ConnectorAble and(QueryBuilder queryBuilder) {
        and();
        return wrap(queryBuilder);
    }

    //TODO 可以断言一定不可能为空白条件
    public void build(BuilderHolder holder) {

        boolean lastIsConnector = false;
        Iterator<Unit> iterator = units.iterator();
        if (iterator.hasNext()) {
            Unit unit = iterator.next();
            unit.handle(holder);
            while (iterator.hasNext()) {
                unit = iterator.next();
                if (!lastIsConnector) {
                    holder.appendWhere(" and ");//自动使用and进行分割
                }
                unit.handle(holder);
                lastIsConnector = unit instanceof Connector;
            }
        }
    }

 /*   public static void main(String[] args) {

        QueryBuilderImpl builder = new QueryBuilderImpl();
        QueryBuilderImpl builder2 = new QueryBuilderImpl();

        builder.eq(new F("name"), "丁常磊").eq(new F("age"), 18).or(
                builder2.eq(new F("name"), "测试")
        );


        BuilderHolder holder = new BuilderHolder();

        builder.build(holder);

        System.out.println(holder.toQl());

        System.out.println(holder.getParams());

    }

    private static class F implements QueryField {

        private String key;

        private F(String key) {
            this.key = key;
        }

        @Override
        public String getKey() {
            return key;
        }
    }*/

}
